// 飞书登录配置 JavaScript

// 切换飞书服务状态
function toggleFeishuStatus() {
    const checkbox = document.getElementById('feishu-enabled');
    const isEnabled = checkbox.checked;
    const form = document.getElementById('feishu-form');
    
    showFeishuMessage(isEnabled ? '正在启用...' : '正在禁用...', 'info');
    
    // 切换表单禁用状态
    if (isEnabled) {
        form.classList.remove('disabled');
    } else {
        form.classList.add('disabled');
    }
    
    // 发送状态更新请求
    fetch('plugin/feishu/toggle-status.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ enabled: isEnabled })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            showFeishuMessage(isEnabled ? '飞书服务已启用' : '飞书服务已禁用', 'success');
        } else {
            showFeishuMessage('状态更新失败：' + data.message, 'error');
            checkbox.checked = !isEnabled;
            if (isEnabled) {
                form.classList.add('disabled');
            } else {
                form.classList.remove('disabled');
            }
        }
    })
    .catch(error => {
        showFeishuMessage('请求失败：' + error.message, 'error');
        checkbox.checked = !isEnabled;
        if (isEnabled) {
            form.classList.add('disabled');
        } else {
            form.classList.remove('disabled');
        }
    });
}

// 保存飞书配置
function saveFeishuConfig() {
    const form = document.getElementById('feishu-form');
    
    // 验证必填项
    if (!form.checkValidity()) {
        showFeishuMessage('请填写所有必填项', 'error');
        form.reportValidity();
        return;
    }
    
    const formData = new FormData(form);
    const data = {};
    
    formData.forEach((value, key) => {
        data[key] = value;
    });
    
    showFeishuMessage('正在保存配置...', 'info');
    
    fetch('plugin/feishu/save-config.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
    .then(response => response.json())
    .then(result => {
        if (result.success) {
            showFeishuMessage('配置保存成功', 'success');
        } else {
            showFeishuMessage('保存失败：' + result.message, 'error');
        }
    })
    .catch(error => {
        showFeishuMessage('请求失败：' + error.message, 'error');
    });
}

// 测试飞书连接
function testFeishuConnection() {
    const form = document.getElementById('feishu-form');
    
    // 验证必填项
    if (!form.checkValidity()) {
        showFeishuMessage('请填写所有必填项', 'error');
        form.reportValidity();
        return;
    }
    
    const formData = new FormData(form);
    const data = {};
    
    formData.forEach((value, key) => {
        data[key] = value;
    });
    
    showFeishuMessage('正在测试连接...', 'info');
    
    fetch('plugin/feishu/test-connection.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
    .then(response => response.json())
    .then(result => {
        if (result.success) {
            showFeishuMessage('连接测试成功！' + (result.message || ''), 'success');
        } else {
            showFeishuMessage('连接测试失败：' + result.message, 'error');
        }
    })
    .catch(error => {
        showFeishuMessage('请求失败：' + error.message, 'error');
    });
}

// 显示消息提示
function showFeishuMessage(message, type) {
    const messageDiv = document.getElementById('feishu-message');
    messageDiv.textContent = message;
    messageDiv.className = 'feishu-message show ' + type;
    
    if (type === 'success') {
        setTimeout(() => {
            messageDiv.classList.remove('show');
        }, 3000);
    }
}

// 暴露到全局作用域
window.toggleFeishuStatus = toggleFeishuStatus;
window.saveFeishuConfig = saveFeishuConfig;
window.testFeishuConnection = testFeishuConnection;
window.showFeishuMessage = showFeishuMessage;
